package gov.fnal.xpath2ldap;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import org.dsmltools.DSMLLDAPConverterWrapper;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPSearchResults;
import com.novell.ldap.resources.ResourcesHandler;
import com.novell.ldap.resources.ExceptionMessages;


/**
 * User: tdh
 * Date: Jan 5, 2009
 * Time: 10:58:30 AM
 *
 */
public class DSMLDOMConverter {

    public static Document resultToDOM(LDAPSearchResults dResults, Document lDoc) throws LDAPException {
        Element lEntriesElem=null;
        if(lDoc==null) {
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            dFactory.setNamespaceAware(true);
            DocumentBuilder dBuilder=null;
            try {
                dBuilder = dFactory.newDocumentBuilder();
            } catch (ParserConfigurationException pce) {
                System.err.println("ParserConfigurationException");
            }
            lDoc  = dBuilder.getDOMImplementation().createDocument(null, "dsml", null);
        } else {
            NodeList entries = lDoc.getElementsByTagName("directory-entries");
            if(entries.getLength()>=1) {
                Node firstone = entries.item(0);
                if (firstone instanceof Element) {
                    lEntriesElem = (Element) firstone;
                }
            }
        }

        if(lEntriesElem==null) {
            lEntriesElem = lDoc.createElement("directory-entries");
            lDoc.getDocumentElement().appendChild(lEntriesElem);
        }

        if ((dResults == null) || (dResults.hasMore() == false)) {
            System.err.println("Search returned no results.");
            return null;
        } else {
            while (dResults.hasMore()) {
                LDAPEntry lEntry = null;
                try {
                    lEntry = dResults.next();
                    if(lEntry==null) {
                        continue;
                    }
                } catch (LDAPException lExcept) {
                    //  Handle style of using exception as a return code. Benign when this application closes connection.
                    if(!lExcept.toString().contains(ResourcesHandler.getMessage(ExceptionMessages.CONNECTION_CLOSED, null))) {
                    System.err.println("LDAPException while looking at results: ");
                    System.err.println(lExcept.toString());
                    //
                        // System.err.println(ResourcesHandler.getMessage(ExceptionMessages.CONNECTION_CLOSED, null));
                }
                    lEntry = null;
                }

                /*******************************************************************/
                /* Add to Element                                                   */
                /*******************************************************************/
                if (lEntry != null) {
                    Node lNode = DSMLLDAPConverterWrapper.entryToNode(lEntry, lDoc);
                    lEntriesElem.appendChild(lNode);
                }
            }
        }

        return lDoc;
    }

}
